package org.oschina.api.user.mail.queue;

import org.oschina.Constants;
import org.oschina.api.user.mail.dao.UserMailDAO;
import org.oschina.api.user.mail.model.UserMail;
import org.oschina.api.user.user.dao.UserDAO;
import org.oschina.queue.BaseQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 用户普通邮件推送队列
 */
@Component
public class UserSimpleMailPushQueue extends BaseQueue {

	@Autowired
	private UserMailDAO userMailDAO;
	@Autowired
	private UserDAO userDAO;
	
	@Scheduled(fixedDelay=1000)
	private synchronized void handle()
	{
		UserMail userMail=(UserMail)get(Constants.QUEUE_USER_SIMPLE_MAIL_PUSH, UserMail.class);
		if(userMail==null || !userDAO.isOnline(userMail.getTarget().getId()))
			return;
		
		userMailDAO.putCache(userMail);
		userMailDAO.pushByUserAndType(userMail.getTarget().getId(), userMail.getType(), userMail.getId());
		userMailDAO.updateCountByType(userMail.getTarget().getId(), userMail.getType(), 1);
		userMailDAO.updateUnReadCount(userMail.getTarget().getId(),userMail.getType(),1);
	}
}